import { IStageAssetsInfo } from "babylon-lib/base/stage";
import { Scene } from "@babylonjs/core/scene";
import { Camera } from "@babylonjs/core/Cameras/camera";
import { Light } from "@babylonjs/core/Lights/light";
import { RawAssetsUrl, RawData } from "../raw-assets-info";
import { GenerateComponent } from './default-stage-components/generate-component';
import { ILoose } from "babylon-lib/tool/utils";
import { AbstractAssetTaskInfo, CubeTextureTaskInfo, GLTFMeshTaskInfo, TextureTaskInfo } from "babylon-lib/loader/asset-task-info";
import * as Data from '../data/data';
import { IActiveDefaultVRExperienceOptions } from 'babylon-lib/xr/vr-container';
import { FreeCamera } from "@babylonjs/core/Cameras/freeCamera";
import { Vector3 } from "@babylonjs/core/Maths/math";
import { SEPARATE_NO_ROTATE_HANDEDNESS_CONFIGURATION } from "babylon-lib/xr/vr-motion/vr-motion-movement";
import { XRStage } from "babylon-lib/base/xr-stage";
import { FreezeCamera } from 'babylon-lib/misc/freeze-camera';
import { WebXRFeatureName } from "@babylonjs/core/XR/webXRFeaturesManager";


class DefaultStage extends XRStage {

    protected get sessionMode(): "immersive-vr" | "immersive-ar" {
        return 'immersive-ar';
    }

    protected isProduction(): boolean {
        return Data.isProduction.value;
    }

    protected appName(): string {
        return RawData.appConfig.name;
    }

    protected assetTaskInfos(): ILoose<AbstractAssetTaskInfo> | Array<IStageAssetsInfo> {
        return {
            changjing: new GLTFMeshTaskInfo(RawAssetsUrl.changjing),

            lightCube: new CubeTextureTaskInfo(
                RawAssetsUrl.lightCubePX,
                RawAssetsUrl.lightCubePY,
                RawAssetsUrl.lightCubePZ,
                RawAssetsUrl.lightCubeNX,
                RawAssetsUrl.lightCubeNY,
                RawAssetsUrl.lightCubeNZ,
            ),

            textureMarker: new TextureTaskInfo(RawAssetsUrl.textureMarker),
        };
    }

    protected getXROptions(): IActiveDefaultVRExperienceOptions {
        return {
            renderingGroupId: 3,
            cameraOptions: {
                position: Data.cameraPosition.value,
                target: Data.cameraTarget.value,
                maxZ: 1000,
                checkCollisions: true,
                applyGravity: true,
                ellipsoid: new Vector3(0.5, 1.6, 0.5),
                ellipsoidOffset: new Vector3(0, 1.6, 0),
                speed: 0.05,
            },
            disableTeleportation: true,
            enableMovement: true,
            movementOptions: {
                customRegistrationConfigurations: SEPARATE_NO_ROTATE_HANDEDNESS_CONFIGURATION,
            },
            optionalFeatures: true,
        };
    }

    protected generateComponents(scenes: Scene[], cameras: Camera[], lights: Light[]): void {
        /**
         * 自定义
         */
        GenerateComponent.initialize(this);
        this.bindData();
        Data.cameraData.reset(true);
        FreezeCamera.freezePosition(this.mainScene);
    }

    public async reset(): Promise<void> {
        await super.reset();
        /**
         * 自定义
         */
        Data.cameraData.reset(true);
        FreezeCamera.freezePosition(this.mainScene);
    }

    public resetSight(): void {
        super.resetSight();
        /**
         * 自定义
         */

    }

    public async changeImmersive(inaction?: boolean | undefined): Promise<void> {
        await super.changeImmersive(inaction);
        /**
         * 自定义
         */
        FreezeCamera.unfreeze();
        inaction ?
            console.log('点击了【稍后进入】或【准备好了】') :
            console.log('点击了【现在进入】');
    }

    private bindData(): void {
        Data.cameraData.onChange(() => {
            this.mainCamera.position = Data.cameraPosition.value;
            (this.mainCamera as FreeCamera).target = Data.cameraTarget.value;
        });
    }

}


export { DefaultStage };